<?php
class UserController extends RestrictedBaseController {
	
	public function index() {
		$this->layout->content = View::make('user.index');
	}
	
	public function getList() {
		$this->layout = null;
	
		$query = User::select()->where('id', '<>', 1)->orderBy('name')->orderBy('surname');
		$pName = Input::get('name');
	
		if (!empty($pName)) {
			$query->whereNested(function($q) {
				$q
					->where('name', 'like', '%' . Input::get('name') . '%')
					->where('surname', 'like', '%' . Input::get('name') . '%', 'or');
			});
		}
	
		return Datatables::of($query)->make(true);
	}
	
	public function create() {
		$this->layout->content = View::make('user.create')->with('privileges', Privilege::where('key', '<>', 'admin')->orderBy('name')->get());
	}
	
	public function store() {
		$validator = Validator::make(Input::all(), User::$rules);
		
		if ($validator->fails()) {
			return Redirect::to('user/create')
				->withErrors($validator)
				->withInput(Input::except('password'));
		} else {
			$user = new User();
			$user->name = Input::get('name');
			$user->surname = Input::get('surname');
			$user->username = Input::get('username');
			$user->password = Hash::make(Input::get('password'));
			$user->save();
			
			$privileges = Input::get('privileges');
			$user->privileges()->sync(empty($privileges) ? array() : $privileges);
			
			Session::flash('message', 'Usuário incluído com sucesso');
			return Redirect::to('user');
		}
	}
	
	public function edit($id) {	
		$user = User::find($id);
		
		$this->layout->content = View::make('user.edit')
			->with('user', $user)
			->with('userPrivileges', $user->privileges()->lists('id'))
			->with('privileges', Privilege::where('key', '<>', 'admin')->orderBy('name')->get())
			->with('isDeletable', $user->isDeletable());
	}
	
	public function update($id) {
		$changePassword = Input::get('changePassword');
		$rules = User::$rules;
		$rules['username'] = $rules['username'] . ',username,' . $id;
		if (!isset($changePassword)) {
			unset($rules['password'], $rules['password_confirmation']);
		}
		$validator = Validator::make(Input::all(), $rules);
		
		if ($validator->fails()) {
			return Redirect::to("user/{$id}/edit")
				->withErrors($validator)
				->withInput(Input::except('password'));
		} else {
			$user = User::find($id);
			$user->name = Input::get('name');
			$user->surname = Input::get('surname');
			$user->username = Input::get('username');
			if (isset($changePassword)) {
				$user->password = Hash::make(Input::get('password'));
			}
			$user->save();

			$privileges = Input::get('privileges');
			$user->privileges()->sync(empty($privileges) ? array() : $privileges);
			
			Session::flash('message', 'Usuário atualizado com sucesso');
			return Redirect::to('user');
		}
	}
	
	public function destroy($id) {
		$user = User::find($id);
		$user->privileges()->detach();
		$user->delete();
		
		Session::flash('message', 'Usuário excluído com sucesso');
		return Redirect::to('user');
	}
	
}